package com.itheima.controller;

import com.itheima.Service.CheckgroupService;
import com.itheima.common.constant.MessageConstant;
import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.QueryPageBean;
import com.itheima.common.entity.Result;
import com.itheima.pojo.CheckGroup;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/checkgroup")
public class CheckgroupController {
    @Autowired
    private CheckgroupService checkgroupService;
    /**
     * 新增检查组
     */
    @PostMapping("/add")
    public Result list(@RequestBody CheckGroup checkGroup,@RequestParam List<Integer>checkitemIds){
        log.info("新增检查组:{}",checkGroup,checkitemIds);
        checkgroupService.list(checkGroup,checkitemIds);
        return new Result(true,MessageConstant.ADD_CHECKGROUP_SUCCESS,null);
    }
    /**
     * 分页查询
     */
    @PostMapping("/findPage")
    public PageResult page(@RequestBody QueryPageBean queryPageBean){
        log.info("分页查询;{}",queryPageBean);
        PageResult pageResult=checkgroupService.pagelist(queryPageBean);
        return pageResult;
    }
    /**
     * 删除检查组
     */
    @GetMapping("/deleteById")
    public Result delect(Integer id){
        log.info("根据id删除:{}",id);
        checkgroupService.delect(id);
        return new Result(true,MessageConstant.DELETE_CHECKGROUP_SUCCESS,null);
    }
    /**
     * 根据套餐id查询关联的检查项id
     */
    @GetMapping("/findCheckGroupIdsBySetmealId")
    public Result<List<Integer>> readstearm(Integer setmealId){
        List<Integer> ARR =checkgroupService.readsteaml(setmealId);
        return Result.success(ARR);
    }
    /**
     * 查询所有检查组
     */
    @GetMapping("/findAll")
    public Result<List<CheckGroup>> readgroup(){
        List<CheckGroup> ARR=checkgroupService.readgroup();
        return Result.success(ARR);
    }
    /**
     * 根据id查询检查组
     */
    @GetMapping("/findById")
    public Result readall(Integer id){
        CheckGroup checkGroup=checkgroupService.readall(id);
        return Result.success(checkGroup);
    }

    /**
     * 修改检查组项目
     */
    @PostMapping("/edit")
    public Result updatecheck(@RequestBody  CheckGroup checkGroup,@RequestParam List<Integer>checkitemIds){
        log.info("修改检查项:{}",checkGroup);
        checkgroupService.updatecheck(checkGroup,checkitemIds);
        return new Result(true, MessageConstant.EDIT_CHECKGROUP_SUCCESS,null);

    }
}
